package cnsukidayo.com.gitee;

import cnsukidayo.com.gitee.utils.Resources;
import org.fisco.bcos.sdk.BcosSDK;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.client.protocol.response.BlockNumber;
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.transaction.manager.AssembleTransactionProcessor;
import org.fisco.bcos.sdk.transaction.manager.TransactionProcessorFactory;
import org.fisco.bcos.sdk.transaction.model.dto.CallResponse;

import java.util.ArrayList;

public class BcosSDKTest {

    private long a = 12L;

    // 获取配置文件路径
    public final String configFile = Resources.getResource("config.toml");
    public void testClient() throws Exception {
        // 初始化BcosSDK
        BcosSDK sdk = BcosSDK.build(configFile);
        // 为群组1初始化client
        Client client = sdk.getClient(1);

        // 获取群组1的块高
        BlockNumber blockNumber = client.getBlockNumber();
        System.out.println(blockNumber.getBlockNumber());

        CryptoKeyPair keyPair = client.getCryptoSuite().createKeyPair();
        AssembleTransactionProcessor transactionProcessor  = TransactionProcessorFactory.createAssembleTransactionProcessor(client, keyPair, Resources.getResource("abi"), "");
        // 部署HelloWorld合约。第一个参数为合约名称，第二个参数为合约构造函数的列表，是List<Object>类型。
//        TransactionResponse response = transactionProcessor.deployByContractLoader("HelloWorld", new ArrayList<>());
//        System.out.println(response);

//        String helloWorldAddress = response.getContractAddress();
        // 创建调用交易函数的参数，此处为传入一个参数 11
//        List<Object> params = new ArrayList<>();
//        params.add("test");
        // 调用HelloWorld合约，合约地址为helloWorldAddress， 调用函数名为『set』，函数参数类型为params
//        TransactionResponse transactionResponse = transactionProcessor.sendTransactionAndGetResponseByContractLoader("HelloWorld", helloWorldAddress, "set", params);
        // 查询HelloWorld合约的『name』函数，合约地址为helloWorldAddress，参数为空
        CallResponse callResponse = transactionProcessor.sendCallByContractLoader("HelloWorld", "0xec9e4672a7aa439334c32daf9bc7b0d6c8e34bb1", "get", new ArrayList<>());
        // print
        System.out.println("callResponse.getReturnCode() = " + callResponse.getReturnCode());
        System.out.println("callResponse.getReturnMessage() = " + callResponse.getReturnMessage());
        System.out.println("callResponse.getValues() = " + callResponse.getValues());
        blockNumber = client.getBlockNumber();
        System.out.println("blockNumber.getBlockNumber() = " + blockNumber.getBlockNumber());



    }
}
